/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.blitzcoder.collide;

import de.blitzcoder.collide.engine.BlitzMaxCompiler;
import de.blitzcoder.collide.engine.FileOpener;
import de.blitzcoder.collide.gui.CodeDocument;
import de.blitzcoder.collide.gui.Document;
import de.blitzcoder.collide.gui.DocumentManager;
import java.io.File;
import java.util.ListIterator;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.gjt.sp.jedit.Registers;

/**
 *
 * @author blitzcoder
 * THIS FILE DUBLICATES FUNCTIONALITY FROM MENU.XML
 * TODO: LOAD ACTIONTOOLBAR FROM XML, TOO
 */
public class MenuWorker {

    public static void createNewFile() {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Speicheort wählen");
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Source Code", "bmx");
        chooser.setFileFilter(filter);

        //Check if Ok
        if (chooser.showSaveDialog(Interface.get()) == JFileChooser.APPROVE_OPTION) {
            // Check if the File exists
            File file = chooser.getSelectedFile();
            if (!file.getName().toLowerCase().endsWith(".bmx")) {
                file = new File(file.getParent(), file.getName() + ".bmx");
            }
            if (!file.exists()) {
                try {
                    file.createNewFile();

                    FileOpener.open(file);

                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(Interface.get(), "Konnte " + file.getAbsolutePath() + " nicht anlegen.");
                }
            } else {
                JOptionPane.showMessageDialog(Interface.get(), "Diese Datei existiert bereits.");
            }

        }
    }

    public static void openDialog() {
        //File Chooser
        JFileChooser chooser = new JFileChooser();

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Source Code", "bmx");
        chooser.setFileFilter(filter);

        if (Config.getProperty("system.lastFolder").length() != 0) {
            chooser.setCurrentDirectory(Config.getFileProperty("system.lastFolder"));
        }

        //Check if Ok
        if (chooser.showOpenDialog(Interface.get()) == JFileChooser.APPROVE_OPTION) {

            Config.setFileProperty("system.lastFolder", chooser.getSelectedFile());

            // Create new CodeEditor
            //new CodeEditor(chooser.getSelectedFile());
            FileOpener.open(chooser.getSelectedFile());

        }

    }

    public static void saveFile() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).saveCode();
        }
    }

    public static void shiftLeft() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().shiftIndentLeft();
        }
    }

    public static void shiftRight() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().shiftIndentRight();
        }
    }

    public static void compile() {
        saveAll();
        CodeDocument doc = CodeDocument.getCompileCode();
        if (doc != null) {
            BlitzMaxCompiler.compile(doc.getFile());
        }
    }

    public static void compileAndRun() {
        saveAll();
        CodeDocument doc = CodeDocument.getCompileCode();
        if (doc != null) {
            BlitzMaxCompiler.compileAndRun(doc.getFile());
        }
    }

    public static void killProcess() {
        CodeDocument doc = CodeDocument.getCompileCode();
        if (doc != null) {
            // TODO reimplement process killing
            //doc.getCompiler().killProcess();
        }
    }

    public static void saveAll() {
        Document[] docs = Interface.getDocumentManager().getAllDocuments();
        for (Document doc:docs) {
            if (doc instanceof CodeDocument) {
                ((CodeDocument)doc).saveCode();
            }
        }
    }

    public static void copy() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            Registers.copy(((CodeDocument) doc).getTextArea(), '$');
        }
    }

    public static void cut() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            Registers.cut(((CodeDocument) doc).getTextArea(), '$');
        }
    }

    public static void delete() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().delete();
        }
    }

    public static void paste() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            Registers.paste(((CodeDocument) doc).getTextArea(), '$');
        }
    }

    public static void undo() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().getBuffer().undo(((CodeDocument) doc).getTextArea());
        }
    }

    public static void redo() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().getBuffer().redo(((CodeDocument) doc).getTextArea());
        }
    }

    public static void selectAll() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().selectAll();
        }
    }

    public static void debugContinue() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            //((CodeDocument) doc).getCompiler().debugContinue();
        }
    }

    public static void debugStepIn() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            //((CodeDocument) doc).getCompiler().debugStepIn();
        }
    }

    public static void debugStepOut() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            //((CodeDocument) doc).getCompiler().debugStepOut();
        }
    }

    public static void debugStepOver() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            //((CodeDocument) doc).getCompiler().debugStepOver();
        }
    }

    public static void find() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            //((CodeDocument) doc).getSearchEngine().show();
            ((CodeDocument) doc).activateSearchBar();
        }
    }

    public static void findNext() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getSearchEngine().findNext();
        }
    }

    public static void surroundCodeWith(String start, String end) {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().surroundWith(start, end);
        }
    }

    public static void blockComment() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).getTextArea().rangeComment();
        }
    }

    public static void profile() {
        Document doc = Interface.getDocumentManager().getCurrentDocument();
        if (doc != null && doc instanceof CodeDocument) {
            ((CodeDocument) doc).saveCode();
            //((CodeDocument) doc).getCompiler().profile();
        }
    }
}
